class Stack {
  constructor(arr = []) {
    this.items = arr;
    this.count = arr.length;
  }
  push(element) {
    this.items[this.count] = element;
    this.count++;
  }
  pop() {
    if (this.isEmpty()) {
      return undefined;
    }
    this.count--;
    const result = this.items[this.count];
    delete this.items[this.count];
    // let result = this.items.splice(this.count,1);
    return result;
  }
  isEmpty() {
    return this.count === 0;
  }
  size() {
    return this.count;
  }
  peek() {
    if (this.isEmpty()) {
      return undefined;
    }
    return this.items[this.count - 1];
  }
  clear() {
    // this.count = 0;
    // this.items = [];
    while (!this.isEmpty()) {
      this.pop();
    }
  }
  toString() {
    if (this.isEmpty()) return "";
    let objString = `${this.items[0]}`;
    for (let i = 1; i < this.count; i++) {
      objString = `${objString},${this.items[i]}`;
    }
    return objString;
  }
}
// 十进制转二进制
function decimalToBinary(decNumber) {
  const remStack = new Stack();
  let number = decNumber;
  let rem;
  let binaryString = "";
  while (number > 0) {
    rem = Math.floor(number % 2);
    remStack.push(rem);
    number = Math.floor(number / 2);
  }
  while (!remStack.isEmpty()) {
    binaryString += remStack.pop().toString();
  }
  return binaryString;
}
console.log(decimalToBinary(233));

// 进制转换算法
function baseConverter(decNumber, base) {
  const remStack = new Stack();
  const digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // {6}
  let number = decNumber;
  let rem;
  let baseString = "";
  if (!(base >= 2 && base <= 36)) {
    return "";
  }
  while (number > 0) {
    rem = Math.floor(number % base);
    remStack.push(rem);
    number = Math.floor(number / base);
  }
  while (!remStack.isEmpty()) {
    baseString += digits[remStack.pop()]; // {7}
  }
  return baseString;
}

console.log(baseConverter(100345, 16));

// let _stack = new Stack();
// console.log(_stack.isEmpty());
// console.log(_stack.size());
// _stack.push(12);
// _stack.push(23);
// console.log(_stack.items);
// console.log(_stack.peek());
// console.log(_stack.size());
// _stack.pop();
// console.log(_stack.items);
// console.log(_stack.peek());
// console.log(_stack.size());
// _stack.push({ name: 123 });
// _stack.push("hhh");
// console.log(_stack.items);
// console.log(_stack.size());
// _stack.clear();
// console.log(_stack.size());
// console.log(_stack.isEmpty());
// console.log(_stack.toString());
